package com.common.frame.widget

import android.text.InputFilter
import android.text.Spanned
import java.lang.StringBuilder
import java.util.regex.Pattern

/**
 *
 * 中文筛选
 */
class ChineseFilter : InputFilter {
    val regex = "[\\u4e00-\\u9fa5]"

    override fun filter(
        source: CharSequence?,
        start: Int,
        end: Int,
        dest: Spanned?,
        dstart: Int,
        dend: Int
    ): CharSequence {
        val result = StringBuilder("")
        source?.forEach{
            if (isChinese(it.toString())) {
                result.append(it)
            }else{
                result.append("")
            }
        }
        return result;
    }


    fun isChinese(c: String): Boolean {
        return Pattern.matches(regex, c)
    }
}